//
//  ByUserChangeNickNameVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/11.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByUserChangeNickNameVC.h"

@interface ByUserChangeNickNameVC ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *button;

@end

@implementation ByUserChangeNickNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBarButtonItem];
    self.title = @"修改昵称";
    
    [self initViews];
    
    // Do any additional setup after loading the view.
}

- (void)initViews{
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    UILabel *titleLabel = [[UILabel alloc] init];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(10);
        make.size.mas_equalTo(CGSizeMake(80, 40));
    }];
    
    titleLabel.font = FONT(15);
    titleLabel.textColor = BY_TEXT_COLOR;
    titleLabel.text = @"昵称";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor whiteColor];
    
    
    
    UITextField *textField = [[UITextField alloc] init];
    [self.view addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_top).offset(0);
        make.left.equalTo(titleLabel.mas_right).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(titleLabel.mas_bottom).offset(0);
    }];
    textField.placeholder = @"请输入昵称";
    textField.font = FONT(14);
    textField.backgroundColor = [UIColor whiteColor];
    textField.delegate = self;
    self.textField = textField;
    
    UILabel *noteLabel = [[UILabel alloc] init];
    [self.view addSubview:noteLabel];
    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.height.mas_equalTo(@50);
    }];
    noteLabel.font = FONT(12);
    noteLabel.textColor = [UIColor lightGrayColor];
    noteLabel.text = @"昵称填写须知：\n昵称支持中文、字母、符号的组合，4-20个字符";
    noteLabel.numberOfLines = 0;
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:noteLabel.text];
    // 段落重排
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 5;// 行间距
    [attrString addAttributes:@{
                                NSKernAttributeName:@1, // 字间距
                                NSParagraphStyleAttributeName:style, // 行布局
                                NSFontAttributeName:FONT(12)
                                }
                        range:NSMakeRange(0, noteLabel.text.length)];
    
    noteLabel.attributedText = attrString;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(50);
        make.right.equalTo(self.view).offset(-50);
        make.top.equalTo(noteLabel.mas_bottom).offset(50);
        make.height.mas_equalTo(@50);
    }];
    button.backgroundColor = rgba(137, 137, 137, 1);
    [button setTitle:@"确认" forState:UIControlStateNormal];
    button.titleLabel.font = FONT(16);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    button.userInteractionEnabled = NO;
    self.button = button;
}

- (void)btnAction:(UIButton *)btn{
    if (self.textField.text.length > 20 || self.textField.text.length < 1) {
        [ByCoreToolAlert showOnlyText:@"昵称长度不符合要求"];
        return;
    }
    
    ByNetParamsModel *paramsModel = [[ByNetParamsModel alloc] init];
    paramsModel.nickname = self.textField.text;
    [ByApiUser userInfoUpdate:paramsModel success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert showSuccessWithText:@"信息更新成功"];
        ByModelUser *user = BY_USER;
        user.nickname = self.textField.text;
        [ByCacheUser setUserInfo:user];
        [self performSelector:@selector(leftItemsAction:) withObject:nil afterDelay:1];
        
    } failure:^(ByNetBaseResult *result){
        
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.textField.text.length > 0) {
        self.button.userInteractionEnabled = YES;
        self.button.backgroundColor = BY_THEME_COLOR;
    }else{
        self.button.backgroundColor = rgba(137, 137, 137, 1);
        self.button.userInteractionEnabled = NO;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
